/**************************************************************************
Copyright (C) jianbo miao Corporation.  All Rights Released.
this is a 3d engin named our_3d_engin.
our_3d_engin feature:
1:there is no float.
2:it do the 3d things all by softwear.
3:base on 1 and 2 , it can execution on arm which have no 3d hardwear accelerate.

  if you have any  suggestion or question,pls contact with me
  mail:miaojb@126.com
  msn:miaojianbo@hotmail.com
  qq:30209027
  
    2008/01/01
***************************************************************************/

#ifdef OUR_MAIN_C
#ifndef OUR_MODULE_C
#define OUR_MODULE_C

#include "our_math.h"
#include "our_main.h"
#include "our_math.h"
#include "our_3d.h"
#include "our_draw32.h"
#include "math.h"

#define U_MAX 63
    
#define V_MAX 63
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

//==================================================================================
//==================================================================================
void OUR_creat_ball(	int point_num,
                    FIX_POINT3D_PTR	point_org,
                    OUR_3D_TRAG_PTR	triangle_work
                    )
{
    int i,j,m,n,r;
    //FIX_POINT3D_PTR point_org=global.point_org;
    //OUR_3D_TRAG_PTR	triangle_work=global.triangle_work;
    
    for (i=0;i<point_num;i++)//Z -> -Z  ; X -> Y
    {
        n=(i)*PI2_MY/((point_num-1)*2);
        r=500*OUR_Fast_Sin(n)>>15;
        n=500*OUR_Fast_Cos(n)>>15;
        for (j=0;j<point_num;j++)
        {
            m=(j)*PI2_MY/(point_num);
            point_org[(i-1)*point_num+j].MM.y=r*OUR_Fast_Sin(m)>>15;
            point_org[(i-1)*point_num+j].MM.x=r*OUR_Fast_Cos(m)>>15;
            point_org[(i-1)*point_num+j].MM.z=n;
        }
    }
    m=0;
    n=0;
    for (i=0;i<point_num-3;i++)
    {
        //n=(i+1);
        for (j=0;j<point_num-1;j++)
        {
            //left hand ax ,point to outside
            //triangle_work[2*n].CC.clor=0x0ff+0x3000*i+0x20*j;
            triangle_work[2*n].p1=n;
            triangle_work[2*n].p2=n+point_num;
            triangle_work[2*n].p3=n+point_num+1;
            
            //triangle_work[n*2+1].CC.clor=0x0ff000+0x300*i+0x20*j;
            triangle_work[n*2+1].p1=n+point_num+1;
            triangle_work[n*2+1].p2=n+1;
            triangle_work[n*2+1].p3=n;
            n++;/**/
        }
        //triangle_work[n*2].CC.clor=0x0ffff;
        triangle_work[n*2].p1=n;
        triangle_work[n*2].p2=n+point_num;
        triangle_work[n*2].p3=n+1;
        
        //triangle_work[n*2+1].CC.clor=0x0ffff;
        triangle_work[n*2+1].p1=n;
        triangle_work[n*2+1].p2=n+1;
        triangle_work[n*2+1].p3=n+1-point_num;
        n++;/**/
    }
}


//==================================================================================
//==================================================================================
void OUR_creat_ball_cube(	OUR_3D_OBJ_PTR obj,int r
                         )
{

    int i,j,k;
    int n,m;
    int uy,vx;
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    OUR_BMP_INFO** bmp_ptr=obj->bmp_info;
    int num=obj->point_num;
#define UV_R 63
    int uv[][3]={	
        {UV_BUILD(1,1),	UV_BUILD(UV_R,1)	,		UV_BUILD(1,UV_R)},
        {UV_BUILD(UV_R,1),		UV_BUILD(UV_R,UV_R),	UV_BUILD(1,UV_R)},  
    };
    int point[][3]={	
        {	1,	-1,	-1	},// 0
        {	1,	0,	1	},// 1
        {	1,	1,	1	},// 2
        
        {	0,	1,	1	},// 1
        {	-1,	1,	1	},// 2
        
        {	-1,	0,	1	},// 1
        {	-1,	-1,	1	},// 2
        
        {	0,	-1,	1	},// 1
    };
    for (i=0;i<LINE+1;i++)// 3 layers
    {		
        n=1200*i-LINE*600;
        if (i==0)
        {
            n+=1000;
        }
        else if (i==LINE)
        {
            n-=1000;
        }
        for (j=0;j<8;j++)
        {
            point_org[(i)*8+j].MM.z=-point[j][0]*1200;
            point_org[(i)*8+j].MM.x=point[j][1]*1200;
            point_org[(i)*8+j].MM.y=-n;
        }
    }
    //ttt
    for (i=1;i<LINE+1;i++)// 3 layers
    {		
        n=500*n-750;
        for (j=0;j<7;j++)
        {
            triangle_work[((i-1)*8+j)*2].p1=(i)*8+j;
            triangle_work[((i-1)*8+j)*2].p2=(i)*8+j-8;
            triangle_work[((i-1)*8+j)*2].p3=(i)*8+j+1;
            
            triangle_work[((i-1)*8+j)*2+1].p1=(i)*8+j-8;
            triangle_work[((i-1)*8+j)*2+1].p2=(i)*8+j-7;
            triangle_work[((i-1)*8+j)*2+1].p3=(i)*8+j+1;
            
            triangle_work[((i-1)*8+j)*2].bmp_index=(j+1)/2+i*4-4;
            triangle_work[((i-1)*8+j)*2+1].bmp_index=(j+1)/2+i*4-4;
            
            uy=bmp_ptr[(j+1)/2+i*4-4]->height-1;
            vx=(1<<bmp_ptr[(j+1)/2+i*4-4]->bits)-1;
            
            triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(1,1);//00
            triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy,1);//01
            triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(1,vx);//02
            
            triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy,1);//10
            triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy,vx);//11
            triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(1,vx);//12
      
        }
        triangle_work[((i-1)*8+j)*2].p1=(i)*8+7;
        triangle_work[((i-1)*8+j)*2].p2=(i)*8-1;
        triangle_work[((i-1)*8+j)*2].p3=(i)*8;
        
        triangle_work[((i-1)*8+j)*2+1].p1=(i)*8-1;
        triangle_work[((i-1)*8+j)*2+1].p2=(i)*8-8;
        triangle_work[((i-1)*8+j)*2+1].p3=(i)*8;
        
        triangle_work[((i-1)*8+j)*2].bmp_index=i*4-4;
        triangle_work[((i-1)*8+j)*2+1].bmp_index=i*4-4;


        uy=bmp_ptr[triangle_work[((i-1)*8+j)*2].bmp_index]->height-1;
        vx=(1<<bmp_ptr[triangle_work[((i-1)*8+j)*2].bmp_index]->bits)-1;
        
        triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(1,1);//00
        triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy,1);//01
        triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(1,vx);//02
        
        triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy,1);//10
        triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy,vx);//11
        triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(1,vx);//12

      
    }
    j=0;//desk
    triangle_work[((i-1)*8+j)*2].p1=0;
    triangle_work[((i-1)*8+j)*2].p2=7;
    triangle_work[((i-1)*8+j)*2].p3=2;
    
    triangle_work[((i-1)*8+j)*2+1].p1=7;
    triangle_work[((i-1)*8+j)*2+1].p2=3;
    triangle_work[((i-1)*8+j)*2+1].p3=2;
    
    triangle_work[((i-1)*8+j)*2].bmp_index=21;
    triangle_work[((i-1)*8+j)*2+1].bmp_index=21;
    
        uy=bmp_ptr[triangle_work[((i-1)*8+j)*2].bmp_index]->height-1;
        vx=(1<<bmp_ptr[triangle_work[((i-1)*8+j)*2].bmp_index]->bits)-1;
        
        triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(1,1);//00
        triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy/2,1);//01
        triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(1,vx);//02
        
        triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy/2,1);//10
        triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy/2,vx);//11
        triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(1,vx);//12
/*    
    triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(1,1);
    triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(200,1);
    triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(1,255);
    
    triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(200,1);
    triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(200,255);
    triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(1,255);
*/    

    j=1;
    triangle_work[((i-1)*8+j)*2].p1=7;
    triangle_work[((i-1)*8+j)*2].p2=6;
    triangle_work[((i-1)*8+j)*2].p3=3;
    
    triangle_work[((i-1)*8+j)*2+1].p1=6;
    triangle_work[((i-1)*8+j)*2+1].p2=4;
    triangle_work[((i-1)*8+j)*2+1].p3=3;
    triangle_work[((i-1)*8+j)*2].bmp_index=21;
    triangle_work[((i-1)*8+j)*2+1].bmp_index=21;

            
        
        triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(uy/2,1);//00
        triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy,1);//01
        triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(uy/2,vx);//02
        
        triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy,1);//10
        triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy,vx);//11
        triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(uy/2,vx);//12
    /*
    triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(200,1);
    triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(399,1);
    triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(200,255);
    
    triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(399,1);
    triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(399,255);
    triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(200,255);
*/
    


//LINE*8
   // uy=99;
   // vx=63;
    j=2;//opt
    triangle_work[((i-1)*8+j)*2].p1=LINE*8+0;
    triangle_work[((i-1)*8+j)*2].p2=LINE*8+2;
    triangle_work[((i-1)*8+j)*2].p3=LINE*8+7;
    
    triangle_work[((i-1)*8+j)*2+1].p1=LINE*8+7;
    triangle_work[((i-1)*8+j)*2+1].p2=LINE*8+2;
    triangle_work[((i-1)*8+j)*2+1].p3=LINE*8+3;
    
            triangle_work[((i-1)*8+j)*2].bmp_index=20;
            triangle_work[((i-1)*8+j)*2+1].bmp_index=20;


         
        uy=bmp_ptr[triangle_work[((i-1)*8+j)*2].bmp_index]->height-1;
        vx=(1<<bmp_ptr[triangle_work[((i-1)*8+j)*2].bmp_index]->bits)-1;
    /*    
        triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(1,1);//00
        triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy/2,1);//01
        triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(1,vx);//02
        
        triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy/2,1);//10
        triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy/2,vx);//11
        triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(1,vx);//12

  */       
    triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(uy,1);
    triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy,vx);
    triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(uy/2,1);
    
    triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy/2,1);
    triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy,vx);
    triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(uy/2,vx);
       
    
    j=3;
    triangle_work[((i-1)*8+j)*2].p1=LINE*8+7;
    triangle_work[((i-1)*8+j)*2].p2=LINE*8+3;
    triangle_work[((i-1)*8+j)*2].p3=LINE*8+6;
    
    triangle_work[((i-1)*8+j)*2+1].p1=LINE*8+6;
    triangle_work[((i-1)*8+j)*2+1].p2=LINE*8+3;
    triangle_work[((i-1)*8+j)*2+1].p3=LINE*8+4;


            triangle_work[((i-1)*8+j)*2].bmp_index=20;
            triangle_work[((i-1)*8+j)*2+1].bmp_index=20;
/*
        triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(uy/2,1);//00
        triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy,1);//01
        triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(uy/2,vx);//02
        
        triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(uy,1);//10
        triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy,vx);//11
        triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(uy/2,vx);//12
 */
    
    triangle_work[((i-1)*8+j)*2].uv1=UV_BUILD(uy/2,1);
    triangle_work[((i-1)*8+j)*2].uv2=UV_BUILD(uy/2,vx/2);
    triangle_work[((i-1)*8+j)*2].uv3=UV_BUILD(1,1);
    
    triangle_work[((i-1)*8+j)*2+1].uv1=UV_BUILD(1,1);
    triangle_work[((i-1)*8+j)*2+1].uv2=UV_BUILD(uy/2,vx/2);
    triangle_work[((i-1)*8+j)*2+1].uv3=UV_BUILD(1,vx);
            
   	
}

//==================================================================================
//==================================================================================
void OUR_creat_cube(	OUR_3D_OBJ_PTR obj,int r)
{
#define CUBE_R 1000
    int i;
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][3]={	
        {	-1,	1,	-1	},// 0
        {	-1,	1,	1	},// 1
        {	1,	1,	1	},// 2
        {	1,	1,	-1	},// 3
        
        {	-1,	-1,	-1	},// 4
        {	-1,	-1,	1	},// 5
        {	1,	-1,	1	},// 6
        {	1,	-1,	-1	},// 7
        
        {	0,	0,	-1	},// 8
        {	0,	0,	1	},// 9
        
        {	0,	-1,	0	},// 10
        {	0,	1,	0	},// 11
        
        {	-1,	0,	0	},// 12
        {	1,	0,	0	},// 13
        
    };
    char triangle[][3]={	
        {	0,	1,	11	},// 0
        {	1,	2,	11	},// 1
        {	2,	3,	11	},// 0
        {	3,	0,	11	},// 1
        
        {	4,	7,	10	},// 2
        {	7,	6,	10	},// 3
        {	6,	5,	10	},// 2
        {	5,	4,	10	},// 3
        
        {	1,	5,	9	},// 4
        {	5,	6,	9	},// 5
        {	6,	2,	9	},// 4
        {	2,	1,	9	},// 5
        
        {	0,	4,	12	},// 6
        {	4,	5,	12	},// 7
        {	5,	1,	12	},// 6
        {	1,	0,	12	},// 7
        
        {	0,	3,	8	},// 4
        {	3,	7,	8	},// 5
        {	7,	4,	8	},// 4
        {	4,	0,	8	},// 5
        
        {	2,	6,	13	},// 6
        {	6,	7,	13	},// 7
        {	7,	3,	13	},// 6
        {	3,	2,	13	},// 7
    };
    //#define UV_R 62
    int uv[][3]={	
        {UV_BUILD(1,1),		UV_BUILD(UV_R,1),	UV_BUILD(UV_R/2,UV_R/2)	},
        {UV_BUILD(UV_R,1),		UV_BUILD(UV_R,UV_R),	UV_BUILD(UV_R/2,UV_R/2)},
        {UV_BUILD(UV_R,UV_R),		UV_BUILD(1,UV_R),	UV_BUILD(UV_R/2,UV_R/2)	},
        {UV_BUILD(1,UV_R),		UV_BUILD(1,1),	UV_BUILD(UV_R/2,UV_R/2)},
        
    };
    
    for(i=0;i<14;i++)
    {
        point_org[i].MM.x= CUBE_R*point[i][0];
        point_org[i].MM.y= CUBE_R*point[i][1];
        point_org[i].MM.z= CUBE_R*point[i][2];
    }
    for(i=0;i<24;i++)
    {
        triangle_work[i].p1=triangle[i][0];
        triangle_work[i].p2=triangle[i][1];
        triangle_work[i].p3=triangle[i][2];
        
        triangle_work[i].uv1=uv[i%4][0];
        triangle_work[i].uv2=uv[i%4][1];
        triangle_work[i].uv3=uv[i%4][2];
    }
    
}
//==================================================================================
//==================================================================================
void OUR_creat_cube_mofang(OUR_3D_OBJ_PTR obj,int CUBE_1R)
{
    //#define CUBE_R 1000
    int i;
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][3]={	
        {	-1,	1,	-1	},// 0
        {	-1,	1,	1	},// 1
        {	1,	1,	1	},// 2
        {	1,	1,	-1	},// 3
        
        {	-1,	-1,	-1	},// 4
        {	-1,	-1,	1	},// 5
        {	1,	-1,	1	},// 6
        {	1,	-1,	-1	},// 7
    };
    char triangle[][3]={	
        {	0,	1,	2	},// 0
        {	0,	2,	3	},// 1
        {	4,	7,	6	},// 2
        {	4,	6,	5	},// 3
        
        {	1,	5,	6	},// 4
        {	1,	6,	2	},// 5
        {	0,	4,	5	},// 6
        {	0,	5,	1	},// 7
        
        {	0,	3,	7	},// 8
        {	0,	7,	4	},// 9
        {	2,	6,	7	},// a
        {	2,	7,	3	},// b
    };
    CLR clr[]={0xff0000,0x00ff00,0x0000ff,0xffff00,0xff00ff,0x00ffff};
    for(i=0;i<8;i++)
    {
        point_org[i].MM.x= CUBE_1R*point[i][0];
        point_org[i].MM.y= CUBE_1R*point[i][1];
        point_org[i].MM.z= CUBE_1R*point[i][2];
    }
    for(i=0;i<12;i++)
    {
        triangle_work[i].p1=triangle[i][0];
        triangle_work[i].p2=triangle[i][1];
        triangle_work[i].p3=triangle[i][2];
        
        obj->clor[i]=clr[i>>1];
        
    }
    
}

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
void OUR_creat_three(OUR_3D_OBJ_PTR obj)
{
#define CUBE_R1 400
#define CUBE_L 3500
    int i;
    //int U_MAX;
    //int V_MAX;
    //U_MAX = obj->bmp_info->width;
    //V_MAX = obj->bmp_info->height;
    
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][3]={	
        {	CUBE_L/2,	CUBE_R1*0.866,    CUBE_R1/2	},// 0  //sin60=0.866
        {	CUBE_L/2,	-CUBE_R1*0.866,  CUBE_R1/2	},// 0  //sin60=0.866
        {	CUBE_L/2,	0,	                    -CUBE_R1	},// 0  //sin60=0.866
        
        {	-CUBE_L/2,	CUBE_R1*0.866,  CUBE_R1/2	},// 0  //sin60=0.866
        {	-CUBE_L/2,	-CUBE_R1*0.866, CUBE_R1/2	},// 0  //sin60=0.866
        {	-CUBE_L/2,	0,	                    -CUBE_R1	},// 0  //sin60=0.866
    };
    char triangle[][3]={	
        {	0,	2,	5	},// 0
        {	0,	5,	3	},// 1
        
        {	2,	1,	4	},// 0
        {	2,	4,	5	},// 1
        
        {	1,	0,	3	},// 0
        {	1,	3,	4	},// 1
        
        
    };
    
#define U_MAX_3 63
    
#define V_MAX_3 255
    
    int uv[][3]={	
        {UV_BUILD(1,1),		UV_BUILD(U_MAX_3,1),	UV_BUILD(U_MAX_3,V_MAX_3)	},
        {UV_BUILD(1,1),		UV_BUILD(U_MAX_3,V_MAX_3),	UV_BUILD(1,V_MAX_3)		},
        //{UV_BUILD(U_MAX_3,V_MAX_3),		UV_BUILD(U_MAX_3,10),	UV_BUILD(10,10)},
        //{UV_BUILD(10,V_MAX_3),		UV_BUILD(U_MAX_3,V_MAX_3),UV_BUILD(10,10)},
    };
    
    CLR clr[]={0xff0000,0x00ff00,0x0000ff,0xffff00,0xff00ff,0x00ffff};
    for(i=0;i<6;i++)
    {
        point_org[i].MM.x= point[i][0];
        point_org[i].MM.y= point[i][1];
        point_org[i].MM.z= point[i][2];
        if(MATERIAL_point_1P==(obj->material &MATERIAL_MASK))
        {
            obj->clor[i]=clr[i];
        }
    }
    for(i=0;i<6;i++)
    {
        triangle_work[i].p1=triangle[i][0];
        triangle_work[i].p2=triangle[i][1];
        triangle_work[i].p3=triangle[i][2];
        
        
        if(MATERIAL_Triangle_T==(obj->material &MATERIAL_MASK))
        {
            obj->clor[i]=clr[i>>1];
        }
        else if(MATERIAL_bmp==(obj->material &MATERIAL_MASK))
        {
            triangle_work[i].uv1=uv[i%2][0];
            triangle_work[i].uv2=uv[i%2][1];
            triangle_work[i].uv3=uv[i%2][2];
            triangle_work[i].bmp_index=i/2;
        }
    }
    
}
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
void OUR_creat_square(OUR_3D_OBJ_PTR obj)
{
#define CUBE_R 1000
    int i;
    //int U_MAX;
    //int V_MAX;
    //U_MAX = obj->bmp_info->width;
    //V_MAX = obj->bmp_info->height;
    
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][3]={	
        {	0,	-1,	0	},// 0
        {	0,	-1,	2	},// 1
        {	0,	1,	2	},// 2
        {	0,	1,	0	},// 3
    };
    char triangle[][3]={	
        {	0,	1,	2	},// 0
        {	0,	2,	3	},// 1
        {	2,	1,	0	},// 0
        {	3,	2,	0	},// 1
    };
    

    
    int uv[][3]={	
        {UV_BUILD(1,1),		UV_BUILD(U_MAX,1),	UV_BUILD(U_MAX,V_MAX)	},
        {UV_BUILD(1,1),		UV_BUILD(U_MAX,V_MAX),	UV_BUILD(1,V_MAX)		},
        {UV_BUILD(U_MAX,V_MAX),		UV_BUILD(U_MAX,1),	UV_BUILD(1,1)},
        {UV_BUILD(1,V_MAX),		UV_BUILD(U_MAX,V_MAX),UV_BUILD(1,1)},
    };
    
    for(i=0;i<4;i++)
    {
        point_org[i].MM.y= CUBE_R*point[i][0];
        point_org[i].MM.x= CUBE_R*point[i][1];
        point_org[i].MM.z= CUBE_R*point[i][2];
    }
    for(i=0;i<4;i++)
    {
        triangle_work[i].p1=triangle[i][0];
        triangle_work[i].p2=triangle[i][1];
        triangle_work[i].p3=triangle[i][2];
        
        
        triangle_work[i].uv1=uv[i][0];
        triangle_work[i].uv2=uv[i][1];
        triangle_work[i].uv3=uv[i][2];
        
    }
    
}
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
void OUR_creat_mg(OUR_3D_OBJ_PTR obj,int r)
{
    //#define CUBE_R 300
    int i;
    //int U_MAX;
    //int V_MAX;
    //U_MAX = obj->bmp_info->width;
    //V_MAX = obj->bmp_info->height;
    
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][2]={
        
        {	2,	0	},// 1
        {	2,	1	},// 2
        {	1,	1	},// 3
        
        {	1,	2	},// 4
        {	2,	2	},// 5
        {	2,	3	},// 6
        {	1,	3	},// 7
        
        {	1,	4	},// 8
        {	2,	4	},// 9
        {	2,	5	},// 10
        
        {	4,	5	},// 11
        {	4,	3	},// 12
        {	5,	3	},// 13
        
        {	5,	2	},// 14
        {	4,	2	},// 15	
        {	4,	0	},// 0
        
    };
    char triangle[][3]={	
        {	0,1,		16	},// 0
        {	16,1,		17	},// 1
    };
    
//#define U_MAX 128
    
//#define V_MAX 128
    
    int uv[][3]={	
        {UV_BUILD(0,0),		UV_BUILD(U_MAX,0),	UV_BUILD(U_MAX,V_MAX)	},
        {UV_BUILD(0,0),		UV_BUILD(U_MAX,V_MAX),	UV_BUILD(0,V_MAX)		},
        {UV_BUILD(U_MAX,V_MAX),		UV_BUILD(U_MAX,0),	UV_BUILD(0,0)},
        {UV_BUILD(0,V_MAX),		UV_BUILD(U_MAX,V_MAX),UV_BUILD(0,0)},
    };
    
    for(i=0;i<obj->point_num/2;i++)
    {
        point_org[i].MM.x= CUBE_R*(point[i][0])-CUBE_R*3;
        point_org[i].MM.y= CUBE_R*(point[i][1])-CUBE_R*3;
        point_org[i].MM.z= -200;
    }
    for(;i<obj->point_num;i++)
    {
        point_org[i].MM.x= CUBE_R*(point[i-obj->point_num/2][0]-3);
        point_org[i].MM.y= CUBE_R*(point[i-obj->point_num/2][1]-3);
        point_org[i].MM.z= 200;
    }
    
    triangle_work[0].p1=triangle[0][0];
    triangle_work[0].p2=triangle[0][1];
    triangle_work[0].p3=triangle[0][2];
    
    triangle_work[1].p1=triangle[1][0];
    triangle_work[1].p2=triangle[1][1];
    triangle_work[1].p3=triangle[1][2];
    for(i=2;i<obj->triangle_num;i++)
    {
        triangle_work[i].p1=triangle_work[i-2].p1+1;
        triangle_work[i].p2=triangle_work[i-2].p2+1;
        triangle_work[i].p3=triangle_work[i-2].p3+1;
        
        
        //triangle_work[i].uv1=uv[i][0];
        //triangle_work[i].uv2=uv[i][1];
        //triangle_work[i].uv3=uv[i][2];
        
    }
    
}

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
void OUR_creat_square_mg(OUR_3D_OBJ_PTR obj,int r)
{
    //#define CUBE_R 1000
    int i;
    //int U_MAX;
    //int V_MAX;
    //U_MAX = obj->bmp_info->width;
    //V_MAX = obj->bmp_info->height;
    
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][3]={	
        {	0,	-1,	0	},// 0
        {	2,	-1,	0	},// 1
        {	2,	1,	0	},// 2
        {	0,	1,	0	},// 3
    };
    char triangle[][3]={	
        {	0,	1,	2	},// 0
        {	0,	2,	3	},// 1
    };
    
//#define U_MAX 128
    
//#define V_MAX 128
    
    int uv[][3]={	
        {UV_BUILD(0,0),		UV_BUILD(U_MAX,0),	UV_BUILD(U_MAX,V_MAX)	},
        {UV_BUILD(0,0),		UV_BUILD(U_MAX,V_MAX),	UV_BUILD(0,V_MAX)		},
        {UV_BUILD(U_MAX,V_MAX),		UV_BUILD(U_MAX,0),	UV_BUILD(0,0)},
        {UV_BUILD(0,V_MAX),		UV_BUILD(U_MAX,V_MAX),UV_BUILD(0,0)},
    };
    
    for(i=0;i<4;i++)//obj->point_num
    {
        point_org[i].MM.y= CUBE_R*point[i][0];
        point_org[i].MM.x= CUBE_R*point[i][1];
        point_org[i].MM.z= CUBE_R*point[i][2];
    }
    for(i=0;i<2;i++)//obj->triangle_num
    {
        triangle_work[i].p1=triangle[i][0];
        triangle_work[i].p2=triangle[i][1];
        triangle_work[i].p3=triangle[i][2];
        
        
        //	triangle_work[i].uv1=uv[i][0];
        //	triangle_work[i].uv2=uv[i][1];
        //	triangle_work[i].uv3=uv[i][2];
        
    }
    
}
void OUR_creat_M(OUR_3D_OBJ_PTR obj,int r)
{
    //#define CUBE_R 300
    int i;
    //int U_MAX;
    //int V_MAX;
    //U_MAX = obj->bmp_info->width;
    //V_MAX = obj->bmp_info->height;
    
    FIX_POINT3D_PTR	point_org=obj->pPoints;
    OUR_3D_TRAG_PTR	triangle_work=obj->pTriangle;
    int point[][2]={	
        {	0,	0},// 0
        {	2,	3},// 1
        {	6,	3},// 2
        {	6,	-3},// 3
        {	3,	-3},// 3				
        {	3,	2},// 0
        {	1,	-3},// 1
        
        {	-1,	-3},// 1			
        {	-3,	2},// 0
        {	-3,	-3},// 3	
        {	-6,	-3},// 3
        {	-6,	3},// 2
        {	-2,	3},// 1				
        {	-0,	0},// 0
    };
    char triangle[][3]={	
        {	1,0,		14	},// 0
        {	1,14,		15	},// 1
    };
    char triangle_2[][3]={	
        {	0,	1,	5	},// 0
        {	1,	2,	5	},// 1
        {	2,	3,	4	},// 0
        {	2,	4,	5	},// 1
        {	6,	0,	5	},// 1
        
        {	0,	6,	7	},// 0
        
        {	0,	7,	8	},// 1
        {	12,	0,	8	},// 1
        {	11,	12,	8	},// 1
        {	9,	11,	8	},// 1
        {	9,	10,	11	},// 1
    };
    
//#define U_MAX 128
    
//#define V_MAX 128
    
    int uv[][3]={	
        {UV_BUILD(0,0),		UV_BUILD(U_MAX,0),	UV_BUILD(U_MAX,V_MAX)	},
        {UV_BUILD(0,0),		UV_BUILD(U_MAX,V_MAX),	UV_BUILD(0,V_MAX)		},
        {UV_BUILD(U_MAX,V_MAX),		UV_BUILD(U_MAX,0),	UV_BUILD(0,0)},
        {UV_BUILD(0,V_MAX),		UV_BUILD(U_MAX,V_MAX),UV_BUILD(0,0)},
    };
    
    for(i=0;i<obj->point_num/2;i++)
    {
        point_org[i].MM.x= CUBE_R*(point[i][0]);
        point_org[i].MM.y= CUBE_R*(point[i][1]);
        point_org[i].MM.z= -200;
    }
    for(;i<obj->point_num;i++)
    {
        point_org[i].MM.x= CUBE_R*(point[i-obj->point_num/2][0]);
        point_org[i].MM.y= CUBE_R*(point[i-obj->point_num/2][1]);
        point_org[i].MM.z= 200;
    }
    
    triangle_work[0].p1=triangle[0][0];
    triangle_work[0].p2=triangle[0][1];
    triangle_work[0].p3=triangle[0][2];
    
    triangle_work[1].p1=triangle[1][0];
    triangle_work[1].p2=triangle[1][1];
    triangle_work[1].p3=triangle[1][2];
    for(i=2;i<28;i++)
    {
        triangle_work[i].p1=triangle_work[i-2].p1+1;
        triangle_work[i].p2=triangle_work[i-2].p2+1;
        triangle_work[i].p3=triangle_work[i-2].p3+1;
        //triangle_work[i].uv1=uv[i][0];
        //triangle_work[i].uv2=uv[i][1];
        //triangle_work[i].uv3=uv[i][2];
        
    }
    for(i=0;i<11;i++)
    {
        triangle_work[i+28].p1=triangle_2[i][0];
        triangle_work[i+28].p2=triangle_2[i][1];
        triangle_work[i+28].p3=triangle_2[i][2];
        //triangle_work[i].uv1=uv[i][0];
        //triangle_work[i].uv2=uv[i][1];
        //triangle_work[i].uv3=uv[i][2];
        
    }
    for(i=0;i<11;i++)
    {
        triangle_work[i+28+11].p1=triangle_2[i][0]+14;
        triangle_work[i+28+11].p2=triangle_2[i][2]+14;
        triangle_work[i+28+11].p3=triangle_2[i][1]+14;
        //triangle_work[i].uv1=uv[i][0];
        //triangle_work[i].uv2=uv[i][1];
        //triangle_work[i].uv3=uv[i][2];
        
    }
    
    
}
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

#endif
#endif


